package com.goodving.guli.bt.weight;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.activity.C3246a;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.ui.WeightGoalSetView;
import com.goodving.guli.bt.utils.C0880d;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.view.BitmapDescriptorFactory;
import com.goodving.guli.view.C4220a;

import cn.com.smartdevices.bracelet.chart.util.C0801q;
import cn.com.smartdevices.bracelet.config.Config;

/**
 * Created by john on 2015/12/9.
 */
public class as extends DialogFragment {
    public static final String f11354a = "WEIGHTTIPSACTIVITY_BMI";
    public static final String f11355b = "WEIGHTTIPSACTIVITY_AGE";
    public static final String f11356c = "WEIGHTTIPSACTIITY_GENDER";
    public static final String f11357d = "WEIGHTTIPSACTIVITY_HEIGHT";
    public static final String f11358e = "WeightTipsFragment";
    private static final int f11359p = 250;
    private WeightGoalSetView f11360f;
    private TextView f11361g;
    private ImageButton f11362h;
    private RelativeLayout f11363i;
    private TextView f11364j;
    private RelativeLayout f11365k;
    private float f11366l;
    private int f11367m;
    private int f11368n;
    private int f11369o;
    private RelativeLayout f11370q;
    private Bitmap f11371r;
    private Bitmap f11372s;
    private Animator f11373t;
    private Animator f11374u;
    private az f11375v;

    public interface az {
        void m15790a();
    }

    public as() {
        this.f11366l = WeightUtil.f11306h;
        this.f11367m = -1;
        this.f11368n = 1;
        this.f11369o = 0;
    }

    class at implements View.OnClickListener {
        final /* synthetic */ as f11376a;

        at(as asVar) {
            this.f11376a = asVar;
        }

        public void onClick(View view) {
            this.f11376a.onCancel(this.f11376a.getDialog());
        }
    }


    private void m15794a(View view) {
        this.f11370q = (RelativeLayout) view.findViewById(R.id.tips_bg);
        this.f11365k = (RelativeLayout) view.findViewById(R.id.other_tips_layout);
        this.f11360f = (WeightGoalSetView) view.findViewById(R.id.weight_tip_bmi_img);
        this.f11364j = (TextView) view.findViewById(R.id.weight_tip_weight_ref_content);
        this.f11361g = (TextView) view.findViewById(R.id.tips_notice);
        View findViewById = view.findViewById(R.id.weight_tip_weight_ref_title);
        View findViewById2 = view.findViewById(R.id.weight_tip_divider4);
        if (Config.m8876h().f4213w.booleanValue()) {
            findViewById.setVisibility(View.INVISIBLE);
            findViewById2.setVisibility(View.INVISIBLE);
            this.f11364j.setVisibility(View.INVISIBLE);
        }
        if (this.f11367m < 7 || this.f11369o < 100) {
            this.f11360f.setVisibility(View.GONE);
            this.f11365k.setVisibility(View.VISIBLE);
            if (this.f11367m < 7) {
                this.f11361g.setText(getString(R.string.cannot_figure_bmi_causedby_age));
            } else if (this.f11369o < 100) {
                this.f11361g.setText(getString(R.string.cannot_figure_bmi_causedby_height));
            }
        } else {
            this.f11360f.setVisibility(View.VISIBLE);
            this.f11365k.setVisibility(View.GONE);
        }
        if (this.f11367m < 7 || this.f11367m > 18) {
            this.f11360f.m15651b(1);
            this.f11364j.setText(getString(R.string.china_adult_overweight_ref));
            this.f11360f.setScaleRange(0.0f, WeightUtil.f11305g, WeightUtil.f11306h, C4220a.bf, BitmapDescriptorFactory.HUE_MAGENTA);
        } else {
            float a = WeightUtil.m15692a(this.f11367m, this.f11368n);
            float r1 = WeightUtil.m15707b(this.f11367m, this.f11368n);
            GuliLog.tracePrintInfo(f11358e, "child min " + a + " max " + r1);
            this.f11364j.setText(getString(R.string.china_child_overweight_ref));
            this.f11360f.m15651b(1);
            this.f11360f.setScaleRange(0.0f, a, r1, BitmapDescriptorFactory.HUE_MAGENTA);
        }
        if (this.f11366l <= 0.0f) {
            this.f11366l = WeightUtil.f11306h;
        }
        this.f11360f.m15647a(this.f11366l);
        this.f11362h = (ImageButton) view.findViewById(R.id.bmi_view_cancel);
        this.f11362h.setOnClickListener(new at(this));
    }

    @SuppressLint({"NewApi"})
    private Bitmap m15796c() {
        if (this.f11371r == null || this.f11371r.isRecycled()) {
            return null;
        }
        int width = this.f11370q.getWidth();
        int height = this.f11370q.getHeight();
        Matrix matrix = new Matrix();
        matrix.setScale(0.14285715f, 0.14285715f);
        Bitmap createBitmap = Bitmap.createBitmap(this.f11371r, 0, 0, width, height, matrix, true);
        this.f11371r.recycle();
        this.f11371r = null;
        Bitmap a = C0880d.m9051a(getActivity(), createBitmap, 25);
        if (a != createBitmap) {
            createBitmap.recycle();
        }
        this.f11370q.setBackground(new BitmapDrawable(getResources(), a));
        return a;
    }

    public void m15798a() {
        m15799a(250);
    }

    class av implements ValueAnimator.AnimatorUpdateListener {
        final /* synthetic */ as f11378a;

        av(as asVar) {
            this.f11378a = asVar;
        }

        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            this.f11378a.f11370q.setAlpha(((Float) valueAnimator.getAnimatedValue()).floatValue());
        }
    }

    public void m15799a(long j) {
        if (this.f11373t == null || !this.f11373t.isRunning()) {
            this.f11370q.getHeight();
            ValueAnimator ofFloat = ValueAnimator.ofFloat(new float[]{0.0f, 1.0f});
            ofFloat.addUpdateListener(new av(this));
            C0801q.m8765a(ofFloat);
            Animator a = C0801q.m8764a();
            a.setInterpolator(new DecelerateInterpolator());
            a.setDuration(j);
            a.start();
            this.f11373t = ofFloat;
        }
    }

    class aw implements ValueAnimator.AnimatorUpdateListener {
        final /* synthetic */ as f11379a;

        aw(as asVar) {
            this.f11379a = asVar;
        }

        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            this.f11379a.f11370q.setAlpha(((Float) valueAnimator.getAnimatedValue()).floatValue());
        }
    }

    public void m15800a(long j, Animator.AnimatorListener animatorListener) {
        if (this.f11374u == null || !this.f11374u.isRunning()) {
            this.f11370q.getHeight();
            ValueAnimator ofFloat = ValueAnimator.ofFloat(new float[]{1.0f, 0.0f});
            ofFloat.addUpdateListener(new aw(this));
            C0801q.m8765a(ofFloat);
            Animator a = C0801q.m8764a();
            a.setInterpolator(new AccelerateInterpolator());
            a.setDuration(j);
            a.addListener(animatorListener);
            a.start();
            this.f11374u = a;
        }
    }

    public void m15801a(Bitmap bitmap) {
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(new float[]{0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f});
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint);
        new BitmapDrawable(bitmap).draw(canvas);
        this.f11371r = bitmap;
    }

    public void m15802a(az azVar) {
        this.f11375v = azVar;
    }

    class ax implements Animator.AnimatorListener {
        final /* synthetic */ as f11380a;

        ax(as asVar) {
            this.f11380a = asVar;
        }

        public void onAnimationCancel(Animator animator) {
        }

        public void onAnimationEnd(Animator animator) {
            f11380a.dismiss();
        }

        public void onAnimationRepeat(Animator animator) {
        }

        public void onAnimationStart(Animator animator) {
        }
    }

    public void m15803b() {
        m15800a(250, new ax(this));
    }

    public void dismiss() {
        if (getActivity() != null) {
            super.dismiss();
            m15803b();
        }
    }

    public void dismissAllowingStateLoss() {
        if (getActivity() != null) {
            super.dismissAllowingStateLoss();
            m15803b();
        }
    }

    class au implements Runnable {
        final /* synthetic */ as f11377a;

        au(as asVar) {
            this.f11377a = asVar;
        }

        public void run() {
            this.f11377a.f11372s = this.f11377a.m15796c();
            this.f11377a.m15798a();
        }
    }

    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        this.f11370q.post(new au(this));
    }

    public void onCancel(DialogInterface dialogInterface) {
        m15803b();
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        int i = R.style.MenuPanel;
        if (C3246a.m22924b(getActivity())) {
            i = R.style.MenuPanelTint;
        }
        setStyle(0, i);
        if (getArguments() != null) {
            this.f11366l = getArguments().getFloat(f11354a);
            this.f11367m = getArguments().getInt(f11355b);
            this.f11368n = getArguments().getInt(f11356c);
            this.f11369o = getArguments().getInt(f11357d);
        }
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.activity_weight_tips, viewGroup, false);
        m15794a(inflate);
        return inflate;
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.f11372s != null) {
            this.f11372s.recycle();
            this.f11372s = null;
        }
    }

    public void onDismiss(DialogInterface dialogInterface) {
        super.onDismiss(dialogInterface);
        if (this.f11375v != null) {
            this.f11375v.m15790a();
        }
    }

    public void onPause() {
        super.onPause();
        C0668a.m8141b(C0668a.aS);
    }

    public void onResume() {
        super.onResume();
        C0668a.m8133a(C0668a.aS);
    }
}
